
const { app, BrowserWindow, Menu, Tray } = require('electron')
const path = require('path')
let tray = null;
const menus = [
  {
    label: '关于',
    role: 'about',
  },
  { type: 'separator' },
  {
    label: '菜单2',
    click: () => {
      console.log('点击了菜单2')
    },
  },
  {
    label: '子菜单',
    submenu: [
      {
        label: '显示窗口',
        click: () => {
          console.log('点击了二级菜单')
        },
      },
      {
        label: '打开控制台',
        role: 'toggleDevTools',
      },
    ],
  },
  { type: 'separator' },
  {
    label: '退出',
    role: 'quit',
  },
]

app.whenReady().then(()=>{
  createTray()
  // const win = new BrowserWindow({
  //   width: 800,
  //   height: 600,
  // })
  // win.loadFile(path.join(__dirname,'../renderer/index.html'))
})

function createTray() {
  tray = new Tray(path.join(__dirname, '../../../doc/logoTemplate.png'))
  const contextMenu = Menu.buildFromTemplate(menus)
  // 设置托盘菜单
  tray.setContextMenu(contextMenu)
  // 设置悬浮提示
  tray.setToolTip('点击查看股市行情')
  // 代码控制，自动弹出托盘菜单
  // setTimeout(() => {
  //   const popUpMenu = Menu.buildFromTemplate(menus.slice(1))
  //   tray.popUpContextMenu(popUpMenu)
  // }, 2000)

  tray.on('drag-end',(event)=>{
    event.preventDefault()
    event.stopPropagation()
    const data = event.dataTransfer
    console.log(event)
  })
}